<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

class OrganizationControllerOrganization extends OrganizationController {

	function __construct() {
		parent::__construct();
		$this->registerTask('add', 'edit');
	}
	
	function display( )
	{
		switch($this->getTask())
		{
			case 'add'     :
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'view'  , 'organization_edit');
				JRequest::setVar( 'edit', false );
			} break;
			
			case 'edit'    :
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'view'  , 'organization_edit');
				JRequest::setVar( 'edit', true );
			} break;
			
			default:
			{
				JRequest::setVar( 'view'  , 'organization');
			} break;
			
		}

		parent::display();
	}
		
	function save() {
		JRequest::checkToken() or die( 'Invalid Token' );
			
		$user = & JFactory::getUser();
		$post	= JRequest::get('post');
		$cid	= JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$post['id'] = (int) $cid[0];		
		$post['alias'] = OrganizationHelper::alias($post['name']);;
		$post['function'] = JRequest::getVar( 'function', '', 'post', 'string', JREQUEST_ALLOWRAW );
		$post['people'] = JRequest::getVar( 'people', '', 'post', 'string', JREQUEST_ALLOWRAW );
		$post['equipment'] = JRequest::getVar( 'equipment', '', 'post', 'string', JREQUEST_ALLOWRAW );
		$post['ability'] = JRequest::getVar( 'ability', '', 'post', 'string', JREQUEST_ALLOWRAW );
		$post['education'] = JRequest::getVar( 'education', '', 'post', 'string', JREQUEST_ALLOWRAW );
		$post['foreign'] = JRequest::getVar( 'foreign', '', 'post', 'string', JREQUEST_ALLOWRAW );
		$post['ideal'] = JRequest::getVar( 'ideal', '', 'post', 'string', JREQUEST_ALLOWRAW );
		$post['file'] = JRequest::getVar( 'file', '', 'post', 'string', JREQUEST_ALLOWRAW );
			
		$model = $this->getModel('organization_edit');
		
		if(!$model->store($post)) {
			JError::raiseError(500, $model->getError() );
		}	
		
		$this->setRedirect('index.php?option=com_organization&view=organization', 'Cán bộ đã được lưu lại');
	}
	
	function remove() {
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Bạn chưa cán bộ nào' ) );
		}

		$model = $this->getModel('organization');
		if(!$model->delete($cid)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}

		$this->setRedirect( 'index.php?option=com_organization&view=organization' );
	}
	
	function publish() {

		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Bạn chưa cán bộ nào' ) );
		}

		$model = $this->getModel('organization');
		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}

		$this->setRedirect( 'index.php?option=com_organization&view=organization' );
	}
	
	function unpublish()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Bạn chưa cán bộ nào' ) );
		}

		$model = $this->getModel('organization');
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}

		$this->setRedirect( 'index.php?option=com_organization&view=organization' );
	}	
	
	function cancel()
	{
		//$msg = JText::_( '' );
		$this->setRedirect( 'index.php?option=com_organization&view=organization' );
	}
}
?>